import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter_base/common/user_helper.dart';

///
/// @DIR_PATH：lib/http
/// @TIME：2024/6/12 15:54
/// @AUTHOR：starr
/// 保存cookie拦截器
class CookieInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    //添加请求信息
    options.headers[HttpHeaders.cookieHeader] =
        UserHelper.getInstance.cookieList;
    handler.next(options);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (response.requestOptions.path.contains("user/login")) {
      dynamic list = response.headers[HttpHeaders.setCookieHeader];
      List<String> cookieList = [];
      if (list is List) {
        for (String? cookie in list) {
          cookieList.add(cookie ?? "");
        }
      }
      UserHelper.getInstance.cookieList = cookieList;
    }
    super.onResponse(response, handler);
  }
}
